package com.huishu.order.config;

import com.alibaba.ttl.threadpool.TtlExecutors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName：TranslationThreadPoolConfig
 * @Author: ljj
 * @Date: 2022/6/22 17:30
 */

@Configuration
public class TranslationThreadPoolConfig {

    @Autowired
    private ThreadConfigEntity threadConfigEntity;

    @Primary
    @Bean(name = "threadPoolTaskExecutor")
    public ExecutorService translationThreadPoolTaskExecutor() {
        return TtlExecutors.getTtlExecutorService(new ThreadPoolExecutor(threadConfigEntity.getCorePoolSize(),
            threadConfigEntity.getMaxPoolSize(), threadConfigEntity.getKeepAliveSeconds(), TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(threadConfigEntity.getQueueCapacity()),
            new ThreadPoolExecutor.CallerRunsPolicy()));
    }
}
